package org.gpswalker.sys.server.servlets;

import java.io.IOException;
import java.io.InputStream;

import org.apache.commons.fileupload.FileItemIterator;
import org.apache.commons.fileupload.FileItemStream;
import org.apache.commons.fileupload.FileUploadBase.SizeLimitExceededException;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.io.IOUtils;
import org.gpswalker.sys.abstractions.AbstractHeaderWritingServlet;
import org.gpswalker.sys.server.components.GpxTrack;
import org.gpswalker.sys.server.components.SVG;
import org.gpswalker.sys.server.misc.Misc;
import org.gpswalker.sys.server.users.CmsUser;
import org.gpswalker.sys.server.users.StoredUser;
import org.jdom.output.XMLOutputter;

public class Index extends AbstractHeaderWritingServlet {
    public static final int MAXUPLOADSIZE = 800000;

    private static final long serialVersionUID = -2750028029472512214L;
    String html = "";

    @Override
    protected void arbeiten() {
	html = "";

	setTitle("firstPage");

	html += "<p>Hello, freaking world</p>";

	String xml = "";

	try {
	    ServletFileUpload upload = new ServletFileUpload();
	    upload.setSizeMax(MAXUPLOADSIZE);

	    try {
		FileItemIterator iterator = upload.getItemIterator(sio
			.getRequest());
		while (iterator.hasNext()) {
		    FileItemStream item = iterator.next();

		    if (item.isFormField()) {
			// out.println("Got a form field: " +
			// item.getFieldName());
		    } else {
			InputStream imgStream = item.openStream();

			// Logger.log(null, "we are in threlj lkasd lkjs");

			// construct our entity objects
			byte[] imageBytes = IOUtils.toByteArray(imgStream);

			xml = new String(imageBytes);

			GpxTrack gt = new GpxTrack(xml);
			p("the length of the track is " + gt.distance());
			p("the name of the track is " + gt.name());
			p("the descripyoin of the trac is " + gt.description());
			p("date " + gt.date());
			p("max Speed is " + gt.maxSpeed());
			p("avg speed is " + gt.avgspeed());

		    }
		}
	    } catch (SizeLimitExceededException e) {
		html += ("You exceeded the maximu size ("
			+ e.getPermittedSize() + ") of the file ("
			+ e.getActualSize() + ")");
	    }
	} catch (Exception ex) {
	    p(Misc.getStackTrace(ex));
	}

	html += "<form action='fileReceivingServlet' method='post'  enctype='multipart/form-data'>"
		+ "<input type='file' name='file3' />"
		+ "<input type='submit' value='>>>' />" + "</form>";

	StoredUser u = CmsUser.user();
	if (u != null) {
	    if (u.typesAndTrackSets != null) {
		p("string in stored user's hashtable is "
			+ u.typesAndTrackSets.get("Hello"));
	    }
	}

	
	SVG sv = new SVG(500, 500);
	double points[] = {1.0, 3.5, 3.6, 76.8, 349.04};
	sv.linearGraph(points);
	html += "\n" + new XMLOutputter().outputString(sv.doc());
//	
//	XMLOutputter outputter = new XMLOutputter();
//	try {
//	    outputter.output(sv.doc(), out);
//	} catch (IOException e) {
//	    System.err.println(e);
//	}
//	
	
	dataModel.put("html", html);

    }

    @Override
    protected String templateName() {
	// TODO Auto-generated method stub
	return "emptyTemplate.ftl";
    }

    private void p(String s) {
	html += "<p>" + s + "</p>";
    }

}
